package com.landiao.ctrler.management;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.landiao.ctrler.BaseCRUDCtrler;
import com.landiao.entities.Role;
import com.landiao.entities.ServiceItem;
import com.landiao.entities.ServiceItemPrice;
import com.landiao.entities.ServiceItemRoleRela;
import com.landiao.entities.ShopGrade;
import com.landiao.service.RoleService;
import com.landiao.service.ServiceItemService;
import com.landiao.service.ShopService;

/**
 * 功能说明:服务项目
 *
 */
@Controller
@RequestMapping("/management/serviceitem")
public class ServiceItemCtrler extends BaseCRUDCtrler<ServiceItem, String> {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private ShopService shopService;
	
	private ServiceItemService getServiceItemService() {
        return (ServiceItemService) baseService;
    }
	
	@Override
	public String add(Model model) {
		// TODO Auto-generated method stub
		Role role = new Role();
		role.setCompId(this.getClientUser().getUser().getCompId());
		List<Role> roles = this.roleService.getListRole(role);
		model.addAttribute("roles", roles);
		List<ShopGrade> shopGrades = this.shopService.getListshopGrade(null);
		model.addAttribute("shopGrades", shopGrades);
		return super.add(model);
	}

	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public String edit(Model model,@PathVariable String id) {
		// TODO Auto-generated method stub
		addModelAttr(id,model);
		model.addAttribute("op", "修改");
		return viewName("edit");
	}
	
	@RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
	public String detail(Model model,@PathVariable String id) {
		// TODO Auto-generated method stub
		addModelAttr(id,model);
		model.addAttribute("op", "详情");
		return viewName("edit");
	}
	
	/**
	 * 修改和详情页面的返回参数
	 * @param id
	 * @param model
	 */
	private void addModelAttr(String id,Model model){
		Role tmpRole = new Role();
		tmpRole.setCompId(this.getClientUser().getUser().getCompId());
		List<Role> roleList = this.roleService.getListRole(tmpRole);
		List<Map<String, Object>> roles = new ArrayList<>(roleList.size());
		model.addAttribute("roles", roles);
		List<ShopGrade> shopGradeList = this.shopService.getListshopGrade(null);
		List<Map<String, Object>> shopGrades = new ArrayList<>(shopGradeList.size());
		ServiceItem t = this.getServiceItemService().getItemWithDetail(id);
		if(t!=null){
			//转换成前台参数
			for(Role role:roleList){
				Map<String, Object> map = new HashMap<>();
				map.put("role", role);
				for(ServiceItemRoleRela roleRela:t.getRoleRelaList()){
					if(roleRela.getRoleId().equals(role.getRoleId())){
						map.put("rela", roleRela);
						break;
					}
				}
				roles.add(map);
			}
			
			//转换成前台参数
			for(ShopGrade grade:shopGradeList){
				Map<String, Object> map = new HashMap<>();
				map.put("grade", grade);
				for(ServiceItemPrice price:t.getPriceList()){
					if(price.getShopGradeId().equals(grade.getShopGradeId())){
						map.put("price", price);
						break;
					}
				}
				shopGrades.add(map);
			}
		}
		model.addAttribute("t",t);
		model.addAttribute("shopGrades", shopGrades);
		model.addAttribute("roles", roles);
	}
	
	@ResponseBody
	@RequestMapping("listitem")
	public List<ServiceItem> listitem(){
		String compId=  this.getClientUser().getUser().getCompId();
		List<ServiceItem> list = getServiceItemService().getAllNormalServiceItem(compId);
		return list;
	}
	
	/**
	 * 验证是否存在
	 * 
	 */
	@RequestMapping("isExist")
	@ResponseBody
	public boolean isExist(ServiceItem item) {
		String id = item.getServiceItemId();
		if(StringUtils.isNotEmpty(id));{
			item.setServiceItemId(null);
		}
		ServiceItem service = this.getServiceItemService().getOne(item);
		if (service == null) {
			return true;
		}else{
			if(service.getServiceItemId().equals(id)){
				return true;
			}else{
				return false;
			}
		}
	}
}
